package common;/*
 *
 * @author lileilei
 * Date: 2019/1/14
 * Time: 11:23
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class SqlRun implements Iterator<Object[]>{
	 ResultSet rs;
	    ResultSetMetaData rd;
	    public  SqlRun(String ip, String port, 
	        String userName, String password, String sql) throws ClassNotFoundException, SQLException {
	        Class.forName("com.mysql.jdbc.Driver");
	        String url = String.format("jdbc:mysql://%s:%s/%s", ip, port,"plan");
	        Connection conn = DriverManager.getConnection(url, userName, password);
	        Statement createStatement = conn.createStatement();
	        rs = createStatement.executeQuery(sql);
	        rd = rs.getMetaData();
	    }
	    @Override
	    public boolean hasNext() {
	        boolean flag = false;
	        try {
	            flag = rs.next();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	        return flag;
	    }
	    @Override
	    public Object[] next() {
	        Map<String, String> data = new HashMap<String, String>();
	        try {
	            for (int i = 1; i <= rd.getColumnCount(); i++) {
	                data.put(rd.getColumnName(i), rs.getString(i));
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	        Object r[] = new Object[1];
	        r[0] = data;
	        return r;
	    }
	    @Override
	    public void remove() {
	        try {
	            rs.close();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	    }
}